<template>
  <BaseMainSlot :show-btn-box="true">
    <template v-slot:btn-box>
      <ActionBar
        :page-no="24"
        :add-active="true"
        :del-active="delActive"
        :has-cancel="false"
        @add="add"
        @del="del"
      />
    </template>
    <template v-slot:search-box>
      <SearchBox @search="search">
        <div class="seach-box">
          <div class="select-box-content">
            <div class="select-box-content-name">工序名称：</div>
            <el-input
              v-model="querylist.productionProcessesName"
              placeholder="请输入工序名称"
              clearable
            />
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">所在产线：</div>
            <el-select
              v-model="querylist.productionLineNo"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </div>
        </div>
      </SearchBox>
    </template>
    <template v-slot:table-box>
      <tdtable
        :page="page"
        :title="title"
        :prop-list="propList"
        :show-select-column="showSelectColumn"
        :show-index-column="showIndexColumn"
        :table-data="tableData"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
        @handleSelectionChange="handleSelectionChange"
      >
        <template v-slot:processesEquMapperList="{row}">
          <div
            v-if="row.productionProcessesNo"
            class="pointer"
            @click="showEqus(row)"
          >
            <i class="el-icon-view"></i>查看设备
          </div>
        </template>
        <template v-slot:handler="{row}">
          <div
            v-if="row.productionProcessesNo"
            class="pointer"
            @click="edit(row)"
          >
            <i class="el-icon-edit"></i>修改
          </div>
        </template>
      </tdtable>
    </template>
    <template v-slot:dialog>
      <AddDialog
        v-if="show"
        :show="show"
        :title-text="titleText"
        :options="options"
        :edit-data="formData"
        @closeDialog="closeDialog"
        @addEvent="addEvent"
      />
      <ShowDialog
        :show="showEqu"
        :title-text="EqutitleText"
        :show-options="showOptions"
        @closeShowDialog="closeShowDialog"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar/index1.vue";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import tdtable from "@/components/table/index.vue";
import AddDialog from "./components/add-process-dialog.vue";
import SearchBox from "@/components/SearchBox/index.vue";
import ShowDialog from "@/components/detailDialog/show-dialog.vue";
import processManageApi from "@/api/processing-management/basic-info-manage/process-manage/index";
// 获取关联产线
import productionLineApi from "@/api/processing-management/basic-info-manage/production-line-manage/index";
import { deepClone } from "@/utils/index";
// import { mymixin } from "@/utils/mixin";
export default {
  name: "EntrustProcessTaskHandle",
  components: {
    BaseMainSlot,
    tdtable,
    AddDialog,
    SearchBox,
    ShowDialog,
    ActionBar
  },
  // mixins: [mymixin],
  data() {
    return {
      delActive: false,
      querylist: {
        productionProcessesName: undefined,
        productionLineNo: undefined
      },
      // 获取修改的formData
      formData: {},
      options: [],
      // 获取查看设备
      showOptions: [],
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      title: "工序管理列表",
      propList: [
        { prop: "productionProcessesName", label: "工序名称", minWidth: "80" },
        {
          prop: "processesEquMapperList",
          label: "关联加工设备",
          minWidth: "80",
          slotName: "processesEquMapperList"
        },
        { prop: "productionLineName", label: "所在产线", minWidth: "80" },
        // { prop: 'imgUrl', label: '工序自定义数据', minWidth: '100', slotName: 'image' },
        { label: "操作", minWidth: "120", slotName: "handler" }
      ],
      showIndexColumn: true,
      showSelectColumn: true,
      tableData: [],
      titleText: "工序管理",
      EqutitleText: "加工设备信息",
      show: false,
      showEqu: false,
      handleSelectList: [],
      handle: ""
    };
  },
  computed: {},
  watch: {},
  mounted() {
    this.getOptions();
    this.queryPageList();
  },
  methods: {
    search() {
      this.queryPageList();
    },
    getOptions() {
      productionLineApi.queryByConditionList().then(res => {
        if (res.code === 1) {
          let list = [];
          // 提供表头搜索数据
          res.data.forEach(item => {
            list.push({
              name: item.productionLineName,
              value: item.productionLineNo,
              Mapperlist: item.lineEquMapperList
            });
          });
          console.log(list);
          this.options = list;
        }
      });
    },
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.queryPageList();
    },

    handleCurrentChange(val) {
      this.page.currentPage = val;
      this.queryPageList();
    },
    // 选中table行点击事件
    handleSelectionChange(val) {
      console.log(val);
      this.handleSelectList = [];
      val.forEach(e => {
        this.handleSelectList.push(e.productionProcessesNo);
      });
      if (this.handleSelectList.length > 0) {
        this.delActive = true;
      } else {
        this.delActive = false;
      }
    },
    queryPageList() {
      // 获取数据
      let param = {
        page: this.page.currentPage,
        size: this.page.pageSize,
        entity: this.querylist
      };
      processManageApi.queryPageList(param).then(res => {
        if (res.code === 1) {
          let data = res.data;
          this.page.total = data.totalElements;
          this.tableData = data.content;
        }
      });
    },
    // 添加物料
    addEvent(formData) {
      let params = formData;
      if (this.handle === "add") {
        processManageApi.add(params).then(res => {
          console.log(res);
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.queryPageList();
          }
        });
      }
      if (this.handle === "edit") {
        processManageApi.edit(params).then(res => {
          console.log(res);
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.queryPageList();
          }
        });
      }
    },
    // 删除供应商
    del() {
      let params = {
        noList: this.handleSelectList
      };
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      })
        .then(() => {
          processManageApi.del(params).then(res => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.queryPageList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    edit(row) {
      this.show = true;
      this.handle = "edit";
      let list = deepClone(row);
      let result = [];
      if (list.processesEquMapperList.length > 0) {
        list.processesEquMapperList.forEach(item => {
          result.push(item.equipmentCode);
        });
      }
      list.processesEquMapperList = result;
      this.formData = list;
    },
    // 展示工序对应的设备
    showEqus(row) {
      this.showEqu = true;
      this.showOptions = row.processesEquMapperList;
    },
    // 显示添加弹出框
    add() {
      this.show = true;
      this.handle = "add";
      this.formData = {};
    },
    closeDialog() {
      this.show = false;
    },
    closeShowDialog() {
      this.showEqu = false;
    }
  }
};
</script>

<style scoped lang="scss">
@import "~@/styles/variables.scss";
.botton {
  background-color: #0091b7;
}
</style>
